package com.qd.common.panda.domain.entity.card;

import com.cdqidi.util.page.BaseQuery;
import com.cdqidi.valid.group.DefaultGroup;
import com.cdqidi.valid.group.DeleteGroup;
import com.cdqidi.valid.group.UpdateGroup;
import com.qd.common.panda.constant.PandaConstant;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.util.StringUtils;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;

/**
 * 卷信息
 *
 * @author sjk
 */
@EqualsAndHashCode(callSuper = true)
@Getter
@Setter
@ToString
public class CardInfoDTO extends BaseQuery {

    /**
     * 主键
     */
    @NotBlank(message = "卷信息ID不能为空", groups = {DeleteGroup.class, UpdateGroup.class})
    @Size(max = 50, message = "卷信息ID长度不能超过{max}位", groups = {DeleteGroup.class, UpdateGroup.class})
    private String cardMsgId;
    /**
     * 兑换的开始时间
     */
    private String useStartTime;
    /**
     * 兑换的结束时间
     */
    private String useEndTime;
    /**
     * 卷有效期
     */
    private Integer volumeValidity;
    /**
     * 使用规则
     */
    @Size(max = 500, message = "使用规则长度不能超过{max}位", groups = {DeleteGroup.class, UpdateGroup.class})
    private String useRule;
    /**
     * 标题
     */
    @NotBlank(message = "标题不能为空", groups = DefaultGroup.class)
    @Size(max = 100, message = "标题长度不能超过{max}位", groups = DefaultGroup.class)
    private String title;
    /**
     * 说明
     */
    @Size(max = 100, message = "说明长度不能超过{max}位", groups = DefaultGroup.class)
    private String note;
    /**
     * 卷金额
     */
    private BigDecimal volumeAmount;
    /**
     * 卷金额显示
     */
    private String volumeAmountDisplay;
    /**
     * 折扣
     */
    private Double discount;
    /**
     * 折扣显示
     */
    private String discountDisplay;
    /**
     * 优惠金额
     */
    private BigDecimal discountAmount;
    /**
     * 优惠金额显示
     */
    private String discountAmountDisplay;
    /**
     * 分配熊猫豆
     */
    private Integer needIntegral;
    /**
     * 卷数量
     */
    @NotNull(message = "卷数量不能为空", groups = DefaultGroup.class)
    private Integer cardQuantity;
    /**
     * 卷总数量
     */
    private Integer totalCardQuantity;
    /**
     * 添加时间
     */
    private LocalDateTime addTime;
    /**
     * 更新时间
     */
    private LocalDateTime updateTime;
    /**
     * 使用品牌
     */
    @Size(max = 50, message = "使用品牌长度不能超过{max}位", groups = DefaultGroup.class)
    private String useBrand;
    /**
     * 使用品牌显示
     */
    private String useBrandDisplay;
    /**
     * 计划ID
     */
    @Size(max = 50, message = "计划ID长度不能超过{max}位", groups = DefaultGroup.class)
    private String planId;
    /**
     * 计划ID显示
     */
    private String planIdDisplay;
    /**
     * 机构ID
     */
    @Size(max = 50, message = "机构ID长度不能超过{max}位", groups = DefaultGroup.class)
    private String orgId;
    /**
     * 机构ID显示
     */
    private String orgIdDisplay;
    /**
     * 操作人
     */
    @Size(max = 50, message = "操作人长度不能超过{max}位", groups = DefaultGroup.class)
    private String operator;
    /**
     * 操作人显示
     */
    private String operatorDisplay;
    /**
     * 卷类型
     */
    @NotBlank(message = "卷类型不能为空", groups = DefaultGroup.class)
    @Size(max = 50, message = "卷类型长度不能超过{max}位", groups = DefaultGroup.class)
    private String cardType;
    /**
     * 卷类型显示
     */
    private String cardTypeDisplay;
    /**
     * 优惠方式
     */
    @NotBlank(message = "优惠方式不能为空", groups = DefaultGroup.class)
    @Size(max = 50, message = "优惠方式长度不能超过{max}位", groups = DefaultGroup.class)
    private String discountMethod;
    /**
     * 优惠方式显示
     */
    private String discountMethodDisplay;

    public String getVolumeAmountDisplay() {
        if (null != volumeAmount && volumeAmount.doubleValue() > 0) {
            return volumeAmount.stripTrailingZeros().toPlainString();
        }
        return null;
    }

    public String getDiscountDisplay() {
        if (null != discount && discount > 0) {
            return BigDecimal.valueOf(discount).multiply(BigDecimal.valueOf(100)).setScale(2, RoundingMode.HALF_UP).stripTrailingZeros().toPlainString();
        } else if (null != discount) {
            return BigDecimal.valueOf(discount).stripTrailingZeros().toPlainString();
        }
        return "0";
    }

    public String getDiscountAmountDisplay() {
        if (null != discountAmount && discountAmount.doubleValue() > 0) {
            return discountAmount.stripTrailingZeros().toPlainString();
        }
        return null;
    }

    public String getUseBrandDisplay() {
        if (StringUtils.hasLength(useBrandDisplay)) {
            return useBrandDisplay;
        }
        if (null != useBrand && useBrand.equals(PandaConstant.ALL)) {
            return "不限品牌";
        }
        return useBrandDisplay;
    }

    @Override
    public void freeData() {
        this.cardMsgId = null;
        this.useStartTime = null;
        this.useEndTime = null;
        this.volumeValidity = null;
        this.useRule = null;
        this.title = null;
        this.volumeAmount = null;
        this.volumeAmountDisplay = null;
        this.discount = null;
        this.discountDisplay = null;
        this.discountAmount = null;
        this.discountAmountDisplay = null;
        this.needIntegral = null;
        this.cardQuantity = null;
        this.totalCardQuantity = null;
        this.addTime = null;
        this.updateTime = null;
        this.useBrand = null;
        this.planId = null;
        this.planIdDisplay = null;
        this.orgId = null;
        this.orgIdDisplay = null;
        this.operator = null;
        this.operatorDisplay = null;
        this.cardType = null;
        this.cardTypeDisplay = null;
        this.discountMethod = null;
        this.discountMethodDisplay = null;
        super.freeData();
    }
}
